/*
************************************************************************************************************************
	Creates yearly data sets bef_red2_`year' for years 2000-2006 with information on:
	- date of birth: foed_dag (variable foed_dag of register BEF)
	- immigrant status: ie_type (variable ie_type  of register BEF)
	- sex: koen (variable koen of register BEF)
	- civil status, start and end of year: civstl, civstf (from variable civst of register BEF)
	- living arrangement, start and end of year: fm_markl, fm_markf (from variable fm_mark of register BEF)
	- children, start and end of year: d_childl, d_childf (from variables mor_id, far_id, foed_dag of register BEF)
	- education, start and end of year: edul, eduf (from variables hfaudd, hf_vfra of register UDDA)

	Registers: Variables (Years) 
	- BEF: pnr, foed_dag, ie_type, koen, civst, fm_mark, mor_id, far_id (2000-2007)
	- UDDA: pnr, hfaudd, hf_vfra (2000-2007)
************************************************************************************************************************
*/

forvalues year=2000/2007 {
local lyear=`year'-1

***************************************
/* Generating highest attained education at start of year */
/* Save in tempfile udd`year' */
***************************************

tempfile udd`year'
use pnr hfaudd hf_vfra using "$rawdata/udda`year'", clear

/*Merging education labels*/
g start=hfaudd
capture drop _merge
merge m:1 start using  "\\SRVFSENAS3\formater\SAS formater i Danmarks Statistik\STATA_datasaet\Uddannelser\n_audd2015_l1l5_k.dta", keep(master match)
capture drop _merge
g edu=AUDD2015_L1L5_K
g edu_date=hf_vfra
keep pnr edu edu_date hfaudd 
save `udd`year''


***************************************
/* Generating number of children at start of year for each father and mother */
/* Save in tempfiles father`year' and mother`year' */
***************************************

local var_parent "far_id mor_id foed_dag`year'"
tempfile father`year'
tempfile mother`year'

use `var_parent' using "$rawdata/bef`year'" , clear
g child=far_id!=.
g foed_dag_child=foed_dag`year' if child==1
sort far_id foed_dag_child
by far_id: g child_num=sum(child)
by far_id: egen children=max(child_num)
keep if child_num==1

rename far_id pnr
keep pnr children foed_dag_child
g children_f=children 
g foed_dag_child_f=foed_dag_child
drop children foed_dag_child
save `father`year''

use `var_parent' using "$rawdata/bef`year'" , clear
g child=mor_id!=.
g foed_dag_child=foed_dag`year' if child==1
sort mor_id foed_dag_child
by mor_id: g child_num=sum(child)
by mor_id: egen children=max(child_num)
keep if child_num==1

rename mor_id pnr
keep pnr children foed_dag_child
g children_m=children 
g foed_dag_child_m=foed_dag_child 
drop children foed_dag_child
save `mother`year''

***************************************
/* Merge number of children at start of year to population characteristics at start of year for each mother and father */
/* Generate indicator d_child for having a child at start of year*/
***************************************

local varlist "pnr foed_dag`year' civst`year' fm_mark`year' ie_type`year' koen`year'"  
use `varlist' using "$rawdata/bef`year'", clear
capture drop _merge
merge 1:1 pnr using `father`year'', keep(master match)

capture drop _merge
merge 1:1 pnr using `mother`year'', keep(master match)

g d_child=children_m!=. | children_f!=.
order children_*, last
egen children=rowmin(children_*)
drop children_*
order foed_dag_child_*, last
egen foed_dag_child=rowmin(foed_dag_child_*)
drop foed_dag_child_*

***************************************
/*Merging highest attained education at start of year to population characteristics at start of year*/
/*Save in dataset bef_red_`year'*/
***************************************

capture drop _merge
merge 1:1 pnr using `udd`year'' , keep(master match)
capture drop _merge

capture drop _merge
save "$temp/bef_red_`year'", replace
}

***************************************
/*Merging bef_red_`year' (year 0) with bef_red_`year' of following year (year 1)*/
/*Start of year 1 = end of year 0*/
/*"l" is added to variables for start of year 0 */
/*"f" is added to variables for end of year 0 */
/*Save in dataset bef_red2_`year'*/
***************************************

forvalues year=2000/2006 {
local fyear=`year'+1

use "$temp/bef_red_`year'", clear
rename (foed_dag`year' alder`year' civst`year' fm_mark`year' d_child edu hfaudd ie_type`year' koen`year') (foed_dagl alderl civstl fm_markl d_childl edul hfauddl ie_type koen)   
capture drop children foed_dag_child edu_date
capture drop _merge

merge 1:1 pnr using "$temp/bef_red_`fyear'" , keepusing(pnr foed_dag`fyear' alder`fyear' civst`fyear' fm_mark`fyear' d_child edu hfaudd) keep(match) 
rename (foed_dag`fyear' alder`fyear' civst`fyear' fm_mark`fyear' d_child ) (foed_dagf alderf civstf  fm_markf d_childf)          
capture drop _merge

g year=`year'

capture drop _merge
save "$temp/bef_red2_`year'", replace
}


